package com.yiqingjing.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import android.app.Activity;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.yiqingjing.activity.fragment.NetSettingFragment;
import com.yiqingjing.activity.fragment.OtherSettingFragment;
import com.yiqingjing.activity.fragment.RingSettingFragment;
import com.yiqingjing.activity.fragment.ScreenSettingFragment;
import com.yiqingjing.activity.fragment.VoiceSettingFragment;
import com.yiqingjing.nextset.GetLocation;

public class DetailSettingActivity extends Activity implements OnClickListener {

	private FragmentManager fManager;
	private FragmentTransaction transaction;

	// 音量控制的fragment
	private VoiceSettingFragment voiceSettingFragment;
	// 铃声控制的fragment
	private RingSettingFragment ringSettingFragment;
	// 屏幕控制的fragment
	private ScreenSettingFragment screenSettingFragment;
	// 网络连接控制的fragment
	private NetSettingFragment netSettingFragment;
	// 其他控制的fragment
	private OtherSettingFragment otherSettingFragment;
	// 保存用户设置
	private HashMap<String, Object> setting;
	// 返回按钮
	private ImageButton back;
	// 保存按键
	private TextView save;
	// 保存当前模式
	private int mode = -1;
	// 图片的id
	private int[] resource = new int[] { R.drawable.wifi, R.drawable.location, R.drawable.time };
	// 自定义图片
	private int[] myResource = new int[] { R.drawable.mysence1,
			R.drawable.mysence2, R.drawable.mysence3 };
	// 随机数产生器
	private Random random;
	// 模式图标
	private ImageView senceIcon;
	// 模式名称
	private TextView name;
	private TextView englistName;
	// 名称底部小图标
	private View smallIcon;
	// header的layout
	private View header;
	// 模式头像选择界面
	private GridView gridView;
	private int[] resources = new int[] { R.drawable.a001, R.drawable.a002,
			R.drawable.a003, R.drawable.a004, R.drawable.a005, R.drawable.a006,
			R.drawable.a007, R.drawable.a008, R.drawable.a009, R.drawable.a010,
			R.drawable.a011, R.drawable.a012 };
	private ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
	// 头像选择框
	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_setting);
		random = new Random();
		for (int i = 0; i < 12; ++i) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("reasource", resources[i]);
			list.add(map);
		}
		initIntent();
		setting = new HashMap<String, Object>();
		// 填充默认setting
		initSetting();
		// 初始化fragment
		initFragment();
		// 更换fragment
		translateFragment();
	}

	private void initIntent() {
		Intent intent = getIntent();
		mode = intent.getIntExtra("mode", -1);
	}

	private void initSetting() {
		back = (ImageButton) findViewById(R.id.detail_back);
		save = (TextView) findViewById(R.id.detail_save);

		senceIcon = (ImageView) findViewById(R.id.sence_icon);
		name = (TextView) findViewById(R.id.sence_name);
		englistName = (TextView) findViewById(R.id.english_name);
		header = findViewById(R.id.image);
		smallIcon = findViewById(R.id.small_icon);
		smallIcon.setVisibility(View.GONE);
		englistName.setText("custom");

		switch (mode) {
		case 2:
			save.setText("定位");
			break;
		case 3:
			save.setText("设置时间");
			break;
		default:
			break;
		}
		initImageView();

		back.setOnClickListener(this);
		save.setOnClickListener(this);
		senceIcon.setOnClickListener(this);
		name.setOnClickListener(this);

		setting.put("mode", mode);
		setting.put("alarm", 3);
		setting.put("media", 3);
		setting.put("gprs", 0);
		setting.put("wifihot", 0);
		setting.put("bluetooth", 0);
		setting.put("wifi", 0);
		setting.put("lightness", 127);
		setting.put("gps", 0);
		setting.put("auto_rotate", 0);
		setting.put("ringmode", 0);
		setting.put("screenmode", 0);
		setting.put("ring", 3);
		setting.put("latitude", 0);
		setting.put("longitude", 0);
		setting.put("hour", 0);
		setting.put("minute", 0);
	}

	private void initImageView() {
		if (mode != 0) {
			header.setBackgroundResource(resource[mode-1]);
		} else {
			header.setBackgroundResource(myResource[random.nextInt(2)]);
		}
		int width = getWindowManager().getDefaultDisplay().getWidth();
		System.out.println(width);
		int height = 4 * width / 9;
		android.view.ViewGroup.LayoutParams params = header.getLayoutParams();
		params.height = height;
		params.width = width;
		header.setLayoutParams(params);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.detail_back:
			finish();
			break;
		case R.id.detail_save:
			System.out.println(setting.toString());
			switch (mode) {
			case 2:
				save.setText("保存");
				ArrayList<Double> result = GetLocation.getCurrentLocation(DetailSettingActivity.this);
				setting.put("latitude", result.get(0));
				setting.put("longtitude", result.get(1));
				mode = -1;
				break;
			case 3:
				new TimePickerDialog(DetailSettingActivity.this,
						new OnTimeSetListener() {

							@Override
							public void onTimeSet(TimePicker arg0, int hour,
									int minute) {
								System.out.println(hour + ":" + minute);
								setting.put("hour", hour);
								setting.put("minute", minute);
							}
						}, 00, 00, true).show();
				break;
			default:
				Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
				saveSettins();
				break;
			}
			break;
		case R.id.sence_icon:
			System.out.println("dialog");
			showGridDialog();
			break;
		default:
			break;
		}
	}

	private void saveSettins() {
		SharedPreferences numPreferences = getSharedPreferences("num", MODE_PRIVATE);
		int num = numPreferences.getInt("num", -1);
		SharedPreferences setPreferences = getSharedPreferences("setting" + num, MODE_PRIVATE);
		Editor editor = setPreferences.edit();
		editor.putInt("alarm", (Integer) setting.get("alarm"));
		editor.putInt("media", (Integer) setting.get("media"));
		editor.putInt("ring", (Integer) setting.get("ring"));
		editor.putInt("gprs", (Integer) setting.get("gprs"));
		editor.putInt("wifi", (Integer) setting.get("wifi"));
		editor.putInt("ringmode", (Integer) setting.get("ringmode"));
		editor.putInt("wifihot", (Integer) setting.get("wifihot"));
		editor.putInt("lightmode", (Integer) setting.get("lightmode"));
		editor.putInt("lightness", (Integer) setting.get("lightness"));
		editor.putInt("bluetooth", (Integer) setting.get("bluetooth"));
		editor.putInt("auto_rotate", (Integer) setting.get("auto_rotate"));
		editor.putInt("gps", (Integer) setting.get("gps"));
		editor.putString("ringuri", (String) setting.get("ringuri"));
		editor.putInt("mode", mode);
		editor.putFloat("latitude", (Float) setting.get("latitude"));
		editor.putFloat("longitude", (Float) setting.get("longitude"));
		editor.putInt("hour", (Integer) setting.get("hour"));
		editor.putInt("minute", (Integer) setting.get("minute"));
		editor.commit();
	}

	private void showGridDialog() {
		View view = getLayoutInflater().inflate(R.layout.icon_choose, null);
		gridView = (GridView) view.findViewById(R.id.icon_choose);
		gridView.setAdapter(new SimpleAdapter(this, list, R.layout.grid_item,
				new String[] { "reasource" }, new int[] { R.id.grid_item }));
		gridView.setFocusable(true);
		gridView.setFocusableInTouchMode(true);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				senceIcon.setImageResource(resources[arg2]);
				dialog.dismiss();
			}

		});
		dialog = new Dialog(this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(view);
		dialog.show();
	}

	private void translateFragment() {
		fManager = getFragmentManager();
		transaction = fManager.beginTransaction();

		transaction.replace(R.id.voiceset, voiceSettingFragment);
		transaction.replace(R.id.ringset, ringSettingFragment);
		transaction.replace(R.id.screenset, screenSettingFragment);
		transaction.replace(R.id.netset, netSettingFragment);
		transaction.replace(R.id.otherset, otherSettingFragment);

		transaction.commit();

	}

	private void initFragment() {
		Bundle bundle = new Bundle();
		bundle.putSerializable("setting", setting);

		voiceSettingFragment = new VoiceSettingFragment();
		screenSettingFragment = new ScreenSettingFragment();
		ringSettingFragment = new RingSettingFragment();
		netSettingFragment = new NetSettingFragment();
		otherSettingFragment = new OtherSettingFragment();

		// 将bundle传递给fragment
		voiceSettingFragment.setArguments(bundle);
		screenSettingFragment.setArguments(bundle);
		ringSettingFragment.setArguments(bundle);
		netSettingFragment.setArguments(bundle);
		otherSettingFragment.setArguments(bundle);
	}

}
